<template>
  <table-layout :haveSider="false" :isStandTable="false">
    <template #headCustom>
      <a-button type="primary" @click="onBack">返回</a-button>
      <!-- <a-button type="primary" style="marginLeft:20px" @click="onSure">确认</a-button> -->
      <!-- <a-button type="primary" style="marginLeft:20px" @click="onReset">重置</a-button> -->
    </template>
    <template #form>
      <div class="parbox">
        <div class="box1">
          <basic-form
            v-if="isExit"
            style="marginTop:50px"
            ref="form"
            :isBtn="false"
            :formInfos="s_FormInfo"
            :formDatas="s_formDatas"
            :rules="s_rules"
            @onSubmit="onSubmit"
            @onChangeType="onChangeType"
          ></basic-form>
        </div>
        <div class="box2">
          <a-button type="primary" @click="onSure" >确认</a-button>
          <a-button type="primary" @click="onBack" style="marginLeft:20px" >取消</a-button>
        </div>
      </div>
    </template>
  </table-layout>
</template>

<script>
import tableLayout from '@/views/app/biz/common/index'
import basicForm from '../common/basicForm'
import { getLicenseByPid } from '@/views/app/biz/regisLicense/regisLicense'
import { getInfoByPid, saveInfo } from './invoiceInfo'
import {s_FormInfo,s_formDatas,s_rules } from './invoiceInfoConfig'


export default {
  components: {
    tableLayout,
    basicForm
  },
  data() {
    return {
      isExit:true,
      loading: false,
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  activated(){
    console.log('activated',this.$route );
    this.backPathName = this.$route.params.pathName ? this.$route.params.pathName : 'invoiceInfoManage'
    //如果传了type值表示为重新点击进去的，需要初始化数据，如果未传值表示缓存在页签中，需路由配置keepAlive
    if (this.$route.params.type=='edit') {
        this.editModal(this.$route.params.pid)
    }
    if(this.$route.params.type=='add'){
        this.addModal()
    }
  },
  methods: {
    onBack() {
      this.$router.push({
        name: this.backPathName,
        params: {
          isJump: false,
          pathName: this.$route.name,
          iscloseMul:true,
        }
      })
    },
    initData(){
      console.log('重置数据');
      Object.assign(this.$data, this.$options.data.call(this))
      this.isExit=false
      setTimeout(()=>{
        this.isExit=true
      },1)
    },
    // 新增按钮 点击了新增
    addModal() {
      console.log('点击了新增',s_FormInfo)
      this.initData()
    },
    // 编辑按钮
    editModal(val) {
      console.log('点击了编辑')
      this.initData()
      this.getSBypid(val)
    },
    onReset(){
      this.$refs.form.resetForm()
    },
    onSure() {
      this.$refs.form.submitForm()
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.onBack()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = {...this.s_formDatas,...res.data}
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      // switch (name) {
      //   case 'formName':
      //     // console.log('aaaaaaaaaaaaaa',name)
      //     // const zhucez = this.getZhuCezxx(val)
      //     break
      // }
    },
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    }
  }
}
</script>

<style></style>
